package com.ddm.controller.vx;

import com.ddm.context.BaseContext;
import com.ddm.exception.DataErrorException;
import com.ddm.result.Result;
import com.ddm.service.vxService.ContestRankService;
import com.ddm.vo.vxVo.ContestRankVO;
import com.ddm.vo.vxVo.ContestRankStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/contest-rank")
@Api(tags = "用户端竞赛排行榜相关接口")
@Slf4j
public class ContestRankController {

    @Autowired
    private ContestRankService contestRankService;

    @GetMapping("/{examId}")
    @ApiOperation("获取竞赛排行榜（用户端）")
    public Result<List<ContestRankVO>> getContestRanking(
            @ApiParam(value = "竞赛ID", required = true) @PathVariable Long examId,
            @ApiParam(value = "限制返回数量，默认50") @RequestParam(defaultValue = "50") Integer limit) {
        log.info("获取竞赛排行榜（用户端），竞赛ID: {}, 限制数量: {}", examId, limit);
        List<ContestRankVO> ranking = contestRankService.getContestRanking(examId, limit);
        
        // 用户端显示完全脱敏的用户名
        ranking.forEach(rank -> {
            if (rank.getOriginalUserName() != null) {
                rank.setUserName(rank.getOriginalUserName().substring(0, 1) + "***");
            }
        });
        
        return Result.success(ranking);
    }

    @GetMapping("/{examId}/statistics")
    @ApiOperation("获取竞赛排行榜统计信息（用户端）")
    public Result<ContestRankStatisticsVO> getContestRankStatistics(
            @ApiParam(value = "竞赛ID", required = true) @PathVariable Long examId) {
        log.info("获取竞赛排行榜统计信息（用户端），竞赛ID: {}", examId);
        return Result.success(contestRankService.getContestRankStatistics(examId));
    }

    @GetMapping("/{examId}/my-rank")
    @ApiOperation("获取我的竞赛排名（用户端）")
    public Result<ContestRankVO> getMyContestRank(
            @ApiParam(value = "竞赛ID", required = true) @PathVariable Long examId) {
        log.info("获取我的竞赛排名（用户端），竞赛ID: {}", examId);
        
        // 获取当前登录用户ID
        Long currentUserId = BaseContext.getCurrentId();
        if (currentUserId == null) {
            throw new DataErrorException("用户未登录或登录已过期");
        }
        
        ContestRankVO myRank = contestRankService.getMyContestRank(examId, currentUserId);
        
        // 用户端显示完全脱敏的用户名
        if (myRank.getOriginalUserName() != null) {
            myRank.setUserName(myRank.getOriginalUserName().substring(0, 1) + "***");
        }
        
        return Result.success(myRank);
    }

    @GetMapping("/overall")
    @ApiOperation("获取综合竞赛排行榜（用户端）")
    public Result<List<ContestRankVO>> getOverallContestRanking(
            @ApiParam(value = "限制返回数量，默认30") @RequestParam(defaultValue = "30") Integer limit) {
        log.info("获取综合竞赛排行榜（用户端），限制数量: {}", limit);
        List<ContestRankVO> ranking = contestRankService.getOverallContestRanking(limit);
        
        // 用户端显示完全脱敏的用户名
        ranking.forEach(rank -> {
            if (rank.getOriginalUserName() != null) {
                rank.setUserName(rank.getOriginalUserName().substring(0, 1) + "***");
            }
        });
        
        return Result.success(ranking);
    }

    @GetMapping("/{examId}/top")
    @ApiOperation("获取竞赛排行榜前10名（用户端）")
    public Result<List<ContestRankVO>> getTopContestRanking(
            @ApiParam(value = "竞赛ID", required = true) @PathVariable Long examId) {
        log.info("获取竞赛排行榜前10名（用户端），竞赛ID: {}", examId);
        List<ContestRankVO> ranking = contestRankService.getContestRanking(examId, 10);
        
        // 用户端显示完全脱敏的用户名
        ranking.forEach(rank -> {
            if (rank.getOriginalUserName() != null) {
                rank.setUserName(rank.getOriginalUserName().substring(0, 1) + "***");
            }
        });
        
        return Result.success(ranking);
    }
} 